// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Chained Crusher Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 44 : TYPE_CHAINEDCRUSHER

// Function declarations
reserve function ChainedCrusher_DebugDraw
reserve function ChainedCrusher_DebugSpawn
reserve function ChainedCrusher_Function104

// Static Values
public value ChainedCrusher_value31 = 0;

// Tables

function ChainedCrusher_DebugDraw
	temp7 = object[0].value17
	temp7 -= ChainedCrusher_value31
	temp6 = temp7
	temp6 /= 7
	temp7 %= 7
	temp5 = object.ypos
	switch temp7
	case 0
		temp5 += 0x700000
		break
	case 1
		temp5 += 0xA00000
		break
	case 2
		temp5 += 0x500000
		break
	case 3
		temp5 += 0x780000
		break
	case 4
		temp5 += 0x380000
		break
	case 5
		temp5 += 0x580000
		break
	case 6
		temp5 += 0xB80000
		break
	end switch
	temp0 = temp5
	temp0 -= object.ypos
	temp0 >>= 16
	temp0 += 4
	temp1 = 257
	temp1 -= temp0
	temp2 = temp0
	temp2 += 18
	FlipSign(temp2)
	EditFrame(5, -4, temp2, 8, temp0, 308, temp1)
	DrawSpriteXY(5, object.xpos, temp5)
	DrawSpriteXY(0, object.xpos, object.ypos)
	switch temp6
	case 0
		DrawSpriteXY(1, object.xpos, temp5)
		DrawSpriteXY(4, object.xpos, temp5)
		break
	case 1
		DrawSpriteXY(2, object.xpos, temp5)
		DrawSpriteXY(4, object.xpos, temp5)
		break
	case 2
	case 3
		DrawSpriteXY(3, object.xpos, temp5)
		break
	end switch
end function


function ChainedCrusher_DebugSpawn
	temp7 = object[0].value17
	temp7 -= ChainedCrusher_value31
	temp6 = temp7
	temp6 /= 7
	temp7 %= 7
	CreateTempObject(TypeName[Chained Crusher], 0, object.xpos, object.ypos)
	object[tempObjectPos].value2 = 0
	object[tempObjectPos].value1 = temp6
	if object[tempObjectPos].value1 == 2
		object[tempObjectPos].state = 4
	end if
	object[tempObjectPos].value3 = object.ypos
	object[tempObjectPos].value4 = object[tempObjectPos].value3
	switch temp7
	case 0
		object[tempObjectPos].value4 += 0x700000
		break
	case 1
		object[tempObjectPos].value4 += 0xA00000
		break
	case 2
		object[tempObjectPos].value4 += 0x500000
		break
	case 3
		object[tempObjectPos].value4 += 0x780000
		break
	case 4
		object[tempObjectPos].value4 += 0x380000
		break
	case 5
		object[tempObjectPos].value4 += 0x580000
		break
	case 6
		object[tempObjectPos].value4 += 0xB80000
		break
	end switch
end function


function ChainedCrusher_Function104
	checkResult = 0
	temp0 = object.xpos
	temp0 >>= 16
	temp0 -= screen.cameraX
	Abs(temp0)
	switch object.value1
	case 0
		temp0 -= 64
		break
	case 1
		temp0 -= 56
		break
	case 2
		temp0 -= 0
		break
	case 3
		temp0 -= 24
		break
	end switch
	if temp0 <= screen.xcenter
		temp1 = object.value4
		temp1 -= object.value3
		temp1 >>= 16
		temp1 += 8
		temp1 >>= 1
		temp0 = object.value3
		temp0 += object.value4
		temp0 >>= 17
		temp0 -= 12
		temp0 -= screen.cameraY
		Abs(temp0)
		temp0 -= temp1
		temp0 -= 40
		if temp0 <= screen.ycenter
			checkResult = 1
		end if
	end if
end function


event ObjectMain
	object.priority = PRIORITY_ACTIVE
	if object.value2 == 1
		if object[+1].propertyValue == 0
			object.value7 = 0
			object.yvel += 0x7000
			object.ypos += object.yvel
			if object.ypos >= object.value4
				if object.yvel > 0x7000
					CallFunction(ChainedCrusher_Function104)
					if checkResult == 1
						PlaySfx(SfxName[Crusher], 0)
					end if
				end if
				object.ypos = object.value4
				object.yvel = 0
			end if
		else
			object.yvel = -0x8000
			object.ypos += object.yvel
			if object.ypos <= object.value3
				object.ypos = object.value3
				object.yvel = 0
			else
				if object.value7 == 0
					temp0 = oscillation
					temp0 &= 15
					if temp0 == 0
						CallFunction(ChainedCrusher_Function104)
						if checkResult == 1
							PlaySfx(SfxName[Chain Pull], 0)
						end if
					end if
				end if
			end if
		end if
	else
		switch object.state
		case 0
			object.value7 = 0
			object.yvel += 0x7000
			object.ypos += object.yvel
			if object.ypos >= object.value4
				if object.yvel > 0x7000
					CallFunction(ChainedCrusher_Function104)
					if checkResult == 1
						PlaySfx(SfxName[Crusher], 0)
					end if
				end if
				object.ypos = object.value4
				object.yvel = 0
				object.state++
			end if
			break
		case 1
			object.value0++
			if object.value0 == 60
				object.value0 = 0
				object.yvel = -0x8000
				object.state++
			end if
			break
		case 2
			object.ypos += object.yvel
			if object.ypos <= object.value3
				object.ypos = object.value3
				object.yvel = 0
				object.state++
			else
				if object.value7 == 0
					temp0 = oscillation
					temp0 &= 15
					if temp0 == 0
						CallFunction(ChainedCrusher_Function104)
						if checkResult == 1
							PlaySfx(SfxName[Chain Pull], 0)
						end if
					end if
				end if
			end if
			break
		case 3
			object.value0++
			if object.value0 == 4
				object.value0 = 0
				object.state = 0
			end if
			break
		case 4
			CallFunction(ChainedCrusher_Function104)
			if checkResult == 1
				object.state = 0
				object.value1++
			end if
			break
		end switch
	end if
	object.value5 = object.ypos
	object.value5 &= -0x10000
	object.value5 -= object.value6
	temp0 = object.ypos
	object.ypos = object.value6
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		CheckEqual(object[currentPlayer].state, PlayerObject_HandleMonikaTeleport)
		temp7 = checkResult
		CheckLower(object[0].value1, 20)
		temp7 &= checkResult
		if temp7 == 0			
			switch object.value1
			case 0
				BoxCollisionTest(C_BOX, object.entityPos, -56, -12, 56, 10, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
				switch checkResult
				case 0
					BoxCollisionTest(C_TOUCH, object.entityPos, -48, 12, 48, 36, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
					if checkResult == 1
						CallFunction(PlayerObject_Hit)
					end if
					break
				case 1
					object[currentPlayer].ypos += object.value5
					break
				case 4
					if object[currentPlayer].gravity == GRAVITY_GROUND
						if object.value5 >= 0
							CallFunction(PlayerObject_Kill)
						end if
					end if
					break
				end switch
				break
			case 1
				BoxCollisionTest(C_BOX, object.entityPos, -48, -12, 48, 10, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
				switch checkResult
				case 0
					BoxCollisionTest(C_TOUCH, object.entityPos, -47, 12, 47, 36, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
					if checkResult == 1
						CallFunction(PlayerObject_Hit)
					end if
					break
				case 1
					object[currentPlayer].ypos += object.value5
					break
				case 4
					if object[currentPlayer].gravity == GRAVITY_GROUND
						if object.value5 >= 0
							CallFunction(PlayerObject_Kill)
						end if
					end if
					break
				end switch
				break
			case 3
				BoxCollisionTest(C_BOX, object.entityPos, -16, -12, 16, 12, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
				if checkResult == 0
					BoxCollisionTest(C_BOX, object.entityPos, -16, -12, 16, 12, currentPlayer, HITBOX_AUTO, -10, HITBOX_AUTO, HITBOX_AUTO)
				end if
				switch checkResult
				case 1
					object[currentPlayer].ypos += object.value5
					break
				case 4
					if object[currentPlayer].gravity == GRAVITY_GROUND
						if object.value5 >= 0
							CallFunction(PlayerObject_Kill)
						end if
					end if
					break
				end switch
				break
			end switch
		end if	
	next
	object.ypos = temp0
	object.value6 = object.ypos
	object.value6 &= -0x10000
	temp0 = object.xpos
	temp0 >>= 16
	temp0 -= screen.cameraX
	Abs(temp0)
	temp0 -= 128
	if temp0 > screen.xcenter
		if object.value2 == 1
			object.ypos = object.value4
		else
			object.ypos = object.value3
		end if
		object.value0 = 0
		if object.value1 >= 2
			object.state = 4
			object.value1 = 2
		else
			object.state = 0
		end if
		object.priority = PRIORITY_ACTIVE_BOUNDS
	end if
end event


event ObjectDraw
	temp0 = object.ypos
	temp0 -= object.value3
	temp0 >>= 16
	temp0 += 4
	temp1 = 257
	temp1 -= temp0
	temp2 = temp0
	temp2 += 18
	FlipSign(temp2)
	EditFrame(5, -4, temp2, 8, temp0, 308, temp1)
	DrawSpriteXY(5, object.xpos, object.value6)
	DrawSpriteXY(0, object.xpos, object.value3)
	switch object.value1
	case 0
		DrawSpriteXY(1, object.xpos, object.value6)
		DrawSpriteXY(4, object.xpos, object.value6)
		break
	case 1
		DrawSpriteXY(2, object.xpos, object.value6)
		DrawSpriteXY(4, object.xpos, object.value6)
		break
	case 2
	case 3
		DrawSpriteXY(3, object.xpos, object.value6)
		break
	end switch
end event


event ObjectStartup
	LoadSpriteSheet("MZ/Objects.gif")
	SpriteFrame(-16, -52, 32, 32, 256, 76)
	SpriteFrame(-56, -20, 112, 32, 143, 180)
	SpriteFrame(-48, -20, 96, 32, 159, 147)
	SpriteFrame(-16, -20, 32, 32, 256, 109)
	SpriteFrame(-44, 12, 87, 32, 199, 256)
	SpriteFrame(-4, -206, 8, 188, 308, 69)
	foreach (TypeName[Chained Crusher], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].value2 = object[arrayPos0].propertyValue
		object[arrayPos0].value2 >>= 7
		object[arrayPos0].value1 = object[arrayPos0].propertyValue
		object[arrayPos0].value1 &= 127
		object[arrayPos0].value1 >>= 4
		object[arrayPos0].value1 %= 3
		if object[arrayPos0].value1 == 2
			object[arrayPos0].state = 4
		end if
		temp0 = object[arrayPos0].propertyValue
		temp0 &= 7
		if temp0 > 6
			temp0 = 0
		end if
		object[arrayPos0].value3 = object[arrayPos0].ypos
		object[arrayPos0].value4 = object[arrayPos0].value3
		switch temp0
		case 0
			object[arrayPos0].value4 += 0x700000
			break
		case 1
			object[arrayPos0].value4 += 0xA00000
			break
		case 2
			object[arrayPos0].value4 += 0x500000
			break
		case 3
			object[arrayPos0].value4 += 0x780000
			break
		case 4
			object[arrayPos0].value4 += 0x380000
			break
		case 5
			object[arrayPos0].value4 += 0x580000
			break
		case 6
			object[arrayPos0].value4 += 0xB80000
			break
		end switch
		if object[arrayPos0].value2 == 1
			object[arrayPos0].ypos = object[arrayPos0].value4
		end if
	next
	temp0 = 0
	ChainedCrusher_value31 = DebugMode_ObjCount
	while temp0 < 21
		SetTableValue(TypeName[Chained Crusher], DebugMode_ObjCount, DebugMode_TypesTable)
		SetTableValue(ChainedCrusher_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
		SetTableValue(ChainedCrusher_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
		DebugMode_ObjCount++
		temp0++
	loop
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
